package leetcode.weekly.week292;

//Solution3Test
public class Solution3 {

	int mod = (int) 1e9 + 7;

	public int countTexts(String pressedKeys) {
		char[] cs = pressedKeys.toCharArray();
		int len = cs.length;
		int[] dp = new int[len];
		for (int i = 0; i < dp.length; i++) {
			char c = cs[i];
			int cur = c - '0';
			long ans = i - 1 >= 0 ? dp[i - 1] : 1;
			if (i - 1 >= 0 && cs[i - 1] == c) {
				ans += i - 2 >= 0 ? dp[i - 2] : 1;
			}
			if (i - 1 >= 0 && cs[i - 1] == c && i - 2 >= 0 && cs[i - 2] == c) {
				ans += i - 3 >= 0 ? dp[i - 3] : 1;
			}
			if (cur == 7 || cur == 9) {
				if (i - 1 >= 0 && cs[i - 1] == c && i - 2 >= 0 && cs[i - 2] == c && i - 3 >= 0 && cs[i - 3] == c) {
					ans += i - 4 >= 0 ? dp[i - 4] : 1;
				}
			}
			dp[i] = (int) (ans % mod);
		}
		return dp[len - 1];
	}

}
